package com.tedu.weibo2.controller;

import com.tedu.weibo2.mapper.IWeiboMapper;
import com.tedu.weibo2.pojo.dto.WeiboDTO;
import com.tedu.weibo2.pojo.entity.User;
import com.tedu.weibo2.pojo.entity.Weibo;
import com.tedu.weibo2.pojo.vo.WeiboDetailVO;
import com.tedu.weibo2.pojo.vo.WeiboVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/v1/weibo")
public class WeiboController {
    @Autowired
    private IWeiboMapper iWeiboMapper;

    @PostMapping("/insert")
    public int insert(@RequestBody WeiboDTO weiboDTO, HttpSession session){
        User user = (User)session.getAttribute("user");
        if(user==null){
            return 2;//未登录，发布失败
        }
        Weibo weibo = new Weibo();
        weibo.setContent(weiboDTO.getContent());
        weibo.setUserId(user.getId());
        iWeiboMapper.insert(weibo);
        return 1;//发布成功

    }

    @GetMapping("/selectIndex")
    public List<WeiboVO> selectAll(){
        return iWeiboMapper.selectAll();
    }

    @GetMapping("/selectById")
    public WeiboDetailVO selectById(Integer id){
        WeiboDetailVO weiboDetailVO = iWeiboMapper.selectById(id);
        return weiboDetailVO;
    }
}
